package com.trjcn.wxshare.remoting;

import com.alibaba.fastjson.JSONObject;
import com.trjcn.wxshare.execute.WXShareExecute;
import com.trjcn.wxshare.reqeust.CustomerMsgReq;
import com.trjcn.wxshare.utils.DataBuildUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;

@Slf4j
public class WXShareRemoting {

    private static  Retrofit retrofit;

    private String type = "jsapi";

    private String grantType;

    private String appId;

    private String secret;

    public WXShareRemoting(String baseUrl, String grantType, String appId, String secret){
        this.appId = appId;
        this.grantType = grantType;
        this.secret = secret;
        this.retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public String getAccessToken(){
        log.info("get access_token.....");
        log.info("grant_type : {}", grantType);
        log.info("appid : {}", appId);
        log.info("secret : {}", secret);
        Call<JSONObject> call =  retrofit.create(WXShareExecute.class).getAccessToken(grantType, appId, secret);
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!retrofitResponse.raw().isSuccessful()) {
            throw new RuntimeException("微信获取access_token 网络异常");
        }
        JSONObject jsonObject = retrofitResponse.body();
        if(!jsonObject.containsKey("access_token")){
            log.error("get access token error:{}", jsonObject);
            throw new RuntimeException("微信获取access_token 网络异常");
        }
        return jsonObject.getString("access_token");
    }

    public JSONObject getTicket(String accessToken){
        log.info("get ticket by access_token : {}", accessToken);
        Call<JSONObject> call =  retrofit.create(WXShareExecute.class).getTicket(accessToken, type);
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!retrofitResponse.raw().isSuccessful()) {
            throw new RuntimeException("微信获取ticket 网络异常");
        }
        return retrofitResponse.body();
    }

    public JSONObject messageCustomSend(String accessToken, CustomerMsgReq customerMsgReq){
        log.info("wx applets send custom message : {}", JSONObject.toJSONString(customerMsgReq));
        RequestBody body =RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSONObject.toJSONString(customerMsgReq));
        Call<JSONObject> call =  retrofit.create(WXShareExecute.class).messageCustomSend(accessToken, body);
        Response<JSONObject> retrofitResponse = null;
        try {
            retrofitResponse = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!retrofitResponse.raw().isSuccessful()) {
            throw new RuntimeException("微信小程序发送客服消息失败");
        }
        return retrofitResponse.body();
    }



    public String getAppId(){
        return appId;
    }
}
